package com.wangzhenyu.learn.test.designPattern

//中介模式

@Suppress("UNUSED")
object SMediatorPattern {

    abstract class Mediator {
        abstract fun contact(message: String, person: Person)
    }

    class MediatorStructure : Mediator() {
        var houseOwner: HouseOwner? = null
        var tenant: Tenant? = null

        override fun contact(message: String, person: Person) {
            when (person) {
                houseOwner -> {
                    tenant?.getMessage(message)
                }
                else -> {
                    houseOwner?.getMessage(message)
                }
            }
        }
    }

    abstract class Person(protected var name: String, protected var mediator: Mediator) {

        abstract fun getMessage(message: String)
    }

    class HouseOwner(name: String, mediator: Mediator) : Person(name, mediator) {

        fun contract(message: String) {
            mediator.contact(message, this)
        }

        override fun getMessage(message: String) {
            println("房主" + name + "获取到的信息：" + message)
        }

    }

    class Tenant(name: String, mediator: Mediator) : Person(name, mediator) {

        fun contact(message: String) {
            mediator.contact(message, this)
        }

        override fun getMessage(message: String) {
            println("租房者" + name + "获取到的信息：" + message)
        }
    }

}